package cn.csc.module.distribute.service.bank;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.distribute.controller.admin.bank.vo.BankInfoCreateReqVO;
import cn.csc.module.distribute.controller.admin.bank.vo.BankInfoExportReqVO;
import cn.csc.module.distribute.controller.admin.bank.vo.BankInfoPageReqVO;
import cn.csc.module.distribute.controller.admin.bank.vo.BankInfoUpdateReqVO;
import cn.csc.module.distribute.convert.bank.BankInfoConvert;
import cn.csc.module.distribute.dal.dataobject.bank.BankInfoDO;
import cn.csc.module.distribute.dal.mysql.bank.BankInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.distribute.enums.ErrorCodeConstants.BANK_INFO_NOT_EXISTS;

/**
 * 银行卡信息 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class BankInfoServiceImpl implements BankInfoService {

    @Resource
    private BankInfoMapper bankInfoMapper;

    @Override
    public BigInteger createBankInfo(BankInfoCreateReqVO createReqVO) {
        // 插入
        BankInfoDO bankInfo = BankInfoConvert.INSTANCE.convert(createReqVO);
        bankInfoMapper.insert(bankInfo);
        // 返回
        return bankInfo.getId();
    }

    @Override
    public void updateBankInfo(BankInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateBankInfoExists(updateReqVO.getId());
        // 更新
        BankInfoDO updateObj = BankInfoConvert.INSTANCE.convert(updateReqVO);
        bankInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteBankInfo(BigInteger id) {
        // 校验存在
        validateBankInfoExists(id);
        // 删除
        bankInfoMapper.deleteById(id);
    }

    private void validateBankInfoExists(BigInteger id) {
        if (bankInfoMapper.selectById(id) == null) {
            throw exception(BANK_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BankInfoDO getBankInfo(BigInteger id) {
        return bankInfoMapper.selectById(id);
    }

    @Override
    public List<BankInfoDO> getBankInfoList(Collection<BigInteger> ids) {
        return bankInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BankInfoDO> getBankInfoPage(BankInfoPageReqVO pageReqVO) {
        return bankInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BankInfoDO> getBankInfoList(BankInfoExportReqVO exportReqVO) {
        return bankInfoMapper.selectList(exportReqVO);
    }

}
